﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console.ViewModel;
using Mimoza.Apps.Console.Commands;
using Mimoza.Apps.Console.Resources;

namespace Mimoza.Apps.Console.View
{
    public class BaseView : UserControl
    {
        protected BaseViewModel m_viewModel = null;
        protected List<ToolStripItem> m_toolStripItemList = new List<ToolStripItem>();
        protected System.Windows.Forms.ContextMenuStrip contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(); 

        protected void command_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            foreach (ToolStripItem item in m_toolStripItemList)
            {
                if (item.Tag == sender)
                {
                    BaseConsoleCommand cmd = sender as BaseConsoleCommand;
                    item.Visible = cmd.Visible;
                    item.Enabled = cmd.Enabled;
                    break;
                }
            }
        }
        protected void command_Click(object sender, EventArgs e)
        {
            Object tag = null;
            if ( sender is ToolStripMenuItem )
            {
                tag = (sender as ToolStripMenuItem ).Tag;
            } 
            else if (  sender is ToolStripButton )
            {
                tag = (sender as ToolStripButton).Tag;
            }

            if (tag != null)
            {
                BaseConsoleCommand cmd = tag as BaseConsoleCommand;
                cmd.Execute();
            }
        }

        protected String GetCommandText(BaseConsoleCommand cmd)
        {
            string className = cmd.GetType().FullName;
            string name = Resource.ResourceManager.GetString(className.Replace('.', '_'));

            if (name != null)
            {
                return name;
            }

            return className;
        }
        protected void AddCommand2Context(BaseViewModel viewModel, ContextMenuStrip context)
        {
            if (viewModel.ContextCommands == null) return;

            foreach (BaseConsoleCommand cmd in viewModel.ContextCommands)
            {
                if (cmd != null)
                {
                    ToolStripMenuItem item = new ToolStripMenuItem();
                    item.Text = GetCommandText(cmd);
                    item.Enabled = cmd.Enabled;
                    item.Visible = cmd.Visible;
                    item.Tag = cmd;
                    item.Click += new System.EventHandler(this.command_Click);

                    if (cmd.IsDefault)
                    {
                        item.Font = new System.Drawing.Font(item.Font, System.Drawing.FontStyle.Bold);
                    }

                    context.Items.Add(item);
                    cmd.PropertyChanged += new PropertyChangedEventHandler(this.command_PropertyChanged);

                    m_toolStripItemList.Add(item);
                }
                else
                {
                    context.Items.Add(new ToolStripSeparator());
                }
            }
        }
        protected void AddCommand2Context(BaseViewModel viewModel, ToolStripSplitButton context)
        {
            if (viewModel.ContextCommands == null) return;

            foreach (BaseConsoleCommand cmd in viewModel.ContextCommands)
            {
                if (cmd != null)
                {
                    ToolStripMenuItem item = new ToolStripMenuItem();
                    item.Text = GetCommandText(cmd);
                    item.Enabled = cmd.Enabled;
                    item.Visible = cmd.Visible;
                    item.Tag = cmd;
                    item.Click += new System.EventHandler(this.command_Click);

                    context.DropDownItems.Add(item);
                    cmd.PropertyChanged += new PropertyChangedEventHandler(this.command_PropertyChanged);

                    m_toolStripItemList.Add(item);
                }
                else
                {
                    context.DropDownItems.Add(new ToolStripSeparator());
                }
            }
        }
        public virtual void OnActivate()
        {
        }
        public virtual void Init(BaseViewModel viewModel)
        { }
        public BaseViewModel ViewModel
        {
            get { return m_viewModel; }
        }
     }
}
